using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Telerik.Web.UI;
using System.Globalization;


public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        RadGrid1.Culture = new CultureInfo("en-US");

        RadGrid1.Rebind();

    }

    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        //if (e.Item is GridDataItem)
        //{
        //    HyperLink editLink = (HyperLink)e.Item.FindControl("EditLink");
        //    editLink.Attributes["href"] = "#";
        //    editLink.Attributes["onclick"] = String.Format("return ShowEditForm('{0}','{1}');", e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["EmployeeID"], e.Item.ItemIndex);
        //}
    }

    protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
    {
        if (e.Argument == "Rebind")
        {
            RadGrid1.MasterTableView.SortExpressions.Clear();
            RadGrid1.MasterTableView.GroupByExpressions.Clear();
            RadGrid1.Rebind();
        }
        else if (e.Argument == "RebindAndNavigate")
        {
            RadGrid1.MasterTableView.SortExpressions.Clear();
            RadGrid1.MasterTableView.GroupByExpressions.Clear();
            RadGrid1.MasterTableView.CurrentPageIndex = RadGrid1.MasterTableView.PageCount - 1;
            RadGrid1.Rebind();
        }
    }
    protected void RadButton1_Click(object sender, EventArgs e)
    {
        ServiceReference1.Product[] ProductList = new ServiceReference1.SampleServiceNameClient().ReadAllProducts();

        // Create a DataTable instance
        DataTable dTbl = new DataTable("myDynamicTable");

        // Create a DataColumn instances

        DataColumn dcProducto = new DataColumn();
        DataColumn dcCategoria = new DataColumn();
        DataColumn dcProveedor = new DataColumn();

        dcProducto.ColumnName = "Producto";
        dcProducto.DataType = Type.GetType("System.String");

        dcCategoria.ColumnName = "Categoria";
        dcCategoria.DataType = Type.GetType("System.String");

        dcProveedor.ColumnName = "Proveedor";
        dcProveedor.DataType = Type.GetType("System.String");


        // Add these DataColumns into the DataTable


        dTbl.Columns.Add(dcProducto);
        dTbl.Columns.Add(dcCategoria);
        dTbl.Columns.Add(dcProveedor);

        RadGrid1.Columns.Clear();

        RadGrid1.Columns.Add(new GridBoundColumn());
        RadGrid1.Columns.Add(new GridBoundColumn());
        RadGrid1.Columns.Add(new GridBoundColumn());

        RadGrid1.Columns[0].HeaderText = "Producto";
        RadGrid1.Columns[1].HeaderText = "Categoria";
        RadGrid1.Columns[2].HeaderText = "Proveedor";

        ((GridBoundColumn)RadGrid1.Columns[0]).DataField = "Producto";
        ((GridBoundColumn)RadGrid1.Columns[1]).DataField = "Categoria";
        ((GridBoundColumn)RadGrid1.Columns[2]).DataField = "Proveedor";

        foreach (ServiceReference1.Product p in ProductList)
        {
            // Create a DataRow Instance from the table we create above, with NewRow();
            DataRow myrow = dTbl.NewRow();

            myrow["Producto"] = p.ProductName;
            myrow["Categoria"] = p.Category.CategoryName;
            myrow["Proveedor"] = p.Supplier.CompanyName;
            // Add the row into the table
            dTbl.Rows.Add(myrow);
        }

        DataSet ds = new DataSet("dsProducto");
        ds.Tables.Add(dTbl);

        RadGrid1.DataSource = dTbl.DefaultView;
        RadGrid1.Columns[0].HeaderText = "Producto";
        RadGrid1.Columns[1].HeaderText = "Categoria";
        RadGrid1.Columns[2].HeaderText = "Proveedor";

        Session["dt"] = dTbl;

        RadGrid1.DataBind();

    }
}